package com.sinog.biz.service.business.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinog.biz.mapper.havexml.other.UserLoginDetailsMapper;
import com.sinog.biz.service.business.UserLoginCheckService;
import com.sinog.biz.util.WorkDayUtil;
import com.sinog.core.domain.User;
import com.sinog.core.domain.UserLoginDetails;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.PageReturnUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @desc 用户检查实现类
 * @author lj
 * @date -
 */
@Service
public class UserLoginCheckServiceImpl implements UserLoginCheckService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserLoginDetailsMapper userLoginDetailsMapper;

    /**
     * 登录失败，次数+1
     * @param userAccount userAccount
     * @param captchaLockNum captchaLockNum
     * @param captchaLockTime captchaLockTime
     * @param singlelLockTime singlelLockTime
     * @param ip ip
     */
    @Override
    public void lockUser(String userAccount,Integer captchaLockNum,Integer captchaLockTime,Integer singlelLockTime,String ip) {
        String user = "userlogin:" + userAccount;
        stringRedisTemplate.opsForValue().increment(user,1);
        long expire = stringRedisTemplate.getExpire(user,TimeUnit.MINUTES);
        if(expire == -1) {
            stringRedisTemplate.expire(user,singlelLockTime,TimeUnit.MINUTES);
        }
        insertUserLoginDetails(userAccount,ip,"2","账号或密码错误");
    }

    /**
     * 获取是否可登录状态
     * @param userAccount userAccount
     * @param captchaLockNum captchaLockNum
     * @param captchaLockTime captchaLockTime
     * @return boolean
     */
    @Override
    public boolean getUserLockState(String userAccount,Integer captchaLockNum,Integer captchaLockTime) {
        boolean flag = false;
        String user = "userlogin:" + userAccount;
        String lockNum = stringRedisTemplate.opsForValue().get(user);
        if(StringUtils.isNotBlank(lockNum) && Integer.parseInt(lockNum) > 0 && Integer.parseInt(lockNum) >= captchaLockNum) {
            long expire = stringRedisTemplate.getExpire(user,TimeUnit.MINUTES);
            if(expire > captchaLockNum) {
                stringRedisTemplate.expire(user,captchaLockTime,TimeUnit.MINUTES);
            }
            flag = true;
        }
        return flag;
    }

    /**
     * 保存用户登录信息
     * @param userAccount userAccount
     * @param ip ip
     * @param loginState loginState
     * @param failCause failCause
     */
    @Override
    public void insertUserLoginDetails(String userAccount,String ip,String loginState,String failCause) {
        User user = userLoginDetailsMapper.getUserByAccount(userAccount);
        if(null != user) {
            Date date = new Date();
            UserLoginDetails loginDetails = new UserLoginDetails();
            loginDetails.setAccount(userAccount);
            loginDetails.setUsername(user.getUsername());
            loginDetails.setDepartid(user.getDepartid());
            loginDetails.setLoginState(loginState);
            loginDetails.setLoginTime(date);
            //判断是否是工作日，1-是 2-否
            boolean isWork = WorkDayUtil.isWorkDay(new SimpleDateFormat("yyyy-MM-dd").format(date));
            loginDetails.setWorkday(isWork ? "1" : "2");
            loginDetails.setIp(ip);
            loginDetails.setRemark(failCause);
            userLoginDetailsMapper.insert(loginDetails);
        }
    }

    /**
     * 用户登录信息列表集合
     * @param text text
     * @return String
     */
    @Override
    public String getLoginDetailsList(String text) {
        Map<String,Object> paramMap = JsonUtil.toMap(text);
        Integer pageIndex = (Integer)paramMap.get("pageIndex");
        Integer pageSize = (Integer)paramMap.get("pageSize");
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        page = page.setRecords(userLoginDetailsMapper.getLoginDetailsList(page,paramMap));
        Map<String,Object> returnMap = PageReturnUtil.pageReturnMap(page);
        return JSON.toJSONStringWithDateFormat(returnMap,"yyyy-MM-dd  HH:mm:ss");
    }
}